<!DOCTYPE html>
<html lang="GB18030" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">

    <link rel="stylesheet" th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.css}"
          href="bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" th:href="@{/css/font-awesome.min.css}" href="css/font-awesome.min.css">
    <link rel="stylesheet" th:href="@{/css/main.css}" href="css/main.css">
    <style>
        .tree li {
            list-style-type: none;
            cursor: pointer;
        }
    </style>
</head>

<body>

<div th:replace="/common/top::navigation"></div>
<div class="container-fluid">
    <div class="row">

        <div th:replace="common/left::leftsidebar('/permission/permissionPage/')"></div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <ol class="breadcrumb">
                <li><a href="#">权限管理</a></li>
                <li><a href="#">许可维护</a></li>
                <li class="active">新增</li>
            </ol>
            <div class="panel panel-default">
                <div class="panel-heading">表单数据
                    <div style="float:right;cursor:pointer;" data-toggle="modal" data-target="#myModal"><i
                            class="glyphicon glyphicon-question-sign"></i></div>
                </div>
                <div class="panel-body">
                    <form id="permissionForm" role="form">
                        <input type="hidden" id="id" th:value="${permission == null}? ${pid}:${permission.id}">
                        <div class="form-group">
                            <label for="name">许可名称</label>
                            <input type="text" class="form-control"
                                   th:value="${permission == null} ? '':${permission.name}" id="name"
                                   placeholder="请输入许可名称">
                        </div>
                        <div class="form-group">
                            <label for="url">链接地址</label>
                            <input type="text" class="form-control"
                                   th:value="${permission == null} ? '':${permission.url}" id="url"
                                   placeholder="请输入链接地址">
                            <p class="help-block label label-warning">请输入合法的链接地址</p>
                        </div>
                        <div class="form-group">
                            <label for="url">图标样式Class</label>
                            <input type="text" class="form-control"
                                   th:value="${permission == null} ? '':${permission.iconClass}" id="iconClass"
                                   placeholder="请输入图表样式">
                            <a class="help-block label label-warning"
                               target="view_window"
                               href="https://v3.bootcss.com/components/#dropdowns">Bootstrap官网图表</a>
                        </div>
                        <button type="button" th:onclick="${permission == null}? 'addPermission()' : 'editPermission()'"
                                onclick="addPermission()" class="btn btn-success"><i
                                class="glyphicon glyphicon-plus"></i> [[${permission == null}? '新增': '修改']]
                        </button>
                        <button type="button" onclick="permissionRest()" class="btn btn-danger"><i
                                class="glyphicon glyphicon-refresh"></i> 重置
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/webjars/jquery/3.3.1/jquery.js}" src="jquery/jquery-2.1.1.min.js"></script>
<script th:src="@{/webjars/bootstrap/3.3.7/js/bootstrap.js}" src="bootstrap/js/bootstrap.min.js"></script>
<script th:src="@{/script/docs.min.js}" src="script/docs.min.js"></script>
<script th:src="@{/webjars/github-com-sentsin-layer/3.0.3/src/layer.js}"></script>

<script type="text/javascript">
    $(function () {
        $(".list-group-item").click(function () {
            if ($(this).find("ul")) {
                $(this).toggleClass("tree-closed");
                if ($(this).hasClass("tree-closed")) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });

    });

    function msg(msg) {
        layer.msg(msg, {icon: 0, time: 2000, anim: 6})
    }
    function editPermission() {
        var name = $("#name").val();
        var url = $("#url").val();
        var iconClass = $("#iconClass").val();
        var id = $("#id").val();
        if ($.trim(name) == "") {
            msg("请输入许可名称");
            return;
        }
        if ($.trim(url) == "") {
            msg("请输入URL地址");
            return;
        }
        if ($.trim(iconClass) == "") {
            msg("请输入图标Class属性");
            return;
        }
        var index;
        $.ajax({
            type: "post",
            url: "/permission/editPermission",
            data: {
                name: name,
                url: url,
                id: id,
                iconClass: iconClass
            },
            beforeSend: function () {
                index = layer.load();
            },
            success: function (result) {
                layer.close(index);
                if (result.success) {
                    layer.msg("修改成功", {time: 1200}, function () {
                        window.location.href = "/permission/permissionPage"
                    });
                } else {
                    layer.msg("修改失败", {icon: 5, time: 2000, anim: 6})
                }
            }
        });
    }

    function addPermission() {
        var name = $("#name").val();
        var url = $("#url").val();
        var iconClass = $("#iconClass").val();
        var pid = $("#id").val();
        if ($.trim(name) == "") {
            msg("请输入许可名称");
            return;
        }
        if ($.trim(url) == "") {
            msg("请输入URL地址");
            return;
        }
        var index;
        $.ajax({
            type: "post",
            url: "/permission/addPermission",
            data: {
                name: name,
                url: url,
                pid: pid,
                iconClass: iconClass
            },
            beforeSend: function () {
                index = layer.load();
            },
            success: function (result) {
                layer.close(index);
                if (result.success) {
                    layer.msg("新增成功", {time: 1200}, function () {
                        window.location.href = "/permission/permissionPage"
                    });
                } else {
                    layer.msg("新增失败", {icon: 5, time: 2000, anim: 6})
                }
            }
        });
    }

    function permissionRest() {
        $("#permissionForm")[0].reset();
    }
</script>
</body>
</html>
